#include "chatinfoframe.h"

ChatInfoFrame::ChatInfoFrame(QString name, QWidget *parent) : name(name) {
    this->setObjectName("chatFrame");
    this->setFrameShape(Panel);
    this->setFrameShadow(Raised);
    this->setMaximumHeight(80);
    QGridLayout *grid = new QGridLayout;

    userHead = new QLabel;
    userHead->setPixmap(QPixmap(":/user-line.png"));
    userHead->setScaledContents(true);
    userHead->setMaximumSize(QSize(40, 40));
    grid->addWidget(userHead, 0, 0, 2, 1);
    QLabel *l1 = new QLabel;
    latestMessageLabel = new QLabel;
    l1->setText(name);
    latestMessageLabel->setText("msg");
    extern QFont ft;
    l1->setFont(ft);
    latestMessageLabel->setFont(ft);
    grid->addWidget(l1, 0, 1);
    grid->addWidget(latestMessageLabel, 1, 1);
    this->setLayout(grid);
}

ChatInfoFrame::~ChatInfoFrame() {}

void ChatInfoFrame::setOnlineStatus(bool status) {
    if (status)
        userHead->setStyleSheet("border:1px solid green");
    else
        userHead->setStyleSheet("");
}

void ChatInfoFrame::setSelectedStatus(bool status){
    if(status){
        this->setFrameShadow(QFrame::Sunken);
        this->setLineWidth(2);
        this->setMessageMarked(false);
    }else{
        this->setLineWidth(1);
        this->setFrameShadow(QFrame::Raised);
    }
}

void ChatInfoFrame::setLatestMessage(QString message) {
    QFontMetrics fm(QFont(latestMessageLabel->font()));
    latestMessageLabel->setText(fm.elidedText(message, Qt::ElideRight, 110));
}

void ChatInfoFrame::setMessageMarked(bool status) {
    if (status)
        latestMessageLabel->setStyleSheet("color:red");
    else
        latestMessageLabel->setStyleSheet("");
}

void ChatInfoFrame::mousePressEvent(QMouseEvent *e) { FramePressed = true; }

void ChatInfoFrame::mouseReleaseEvent(QMouseEvent *e) {
    if (FramePressed) {
        emit clicked();
        FramePressed = false;
    }
}
